package influx;

import org.influxdb.InfluxDB;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


/**
 * @author Administrator
 */
class InfluxdbCon {

    public InfluxdbService influxCon() {

        Properties proper = new Properties();

        //加载influxdb数据库的配置文件
        InfluxdbService service = null;
        try {
            try (InputStream ins = new FileInputStream("/neo4j-agent/conf/influxdb.properties")) {
                proper.load(ins);
            }

            //获取数据库的检测ip
            String url = proper.getProperty("influxdb.url");
            String user = proper.getProperty("influxdb.user");
            String password = proper.getProperty("influxdb.password");
            //数据库名
            String dbname = proper.getProperty("AGENT.DB");
            String retentionPolicy = proper.getProperty("NEO4J.RETENTIONPOLICY");
            String time = proper.getProperty("STORAGETIME");

            //建立influxdb连接
            InfluxDB influxDB = InfluxDBUtils.getSingleton(user, password, url);
            service = new InfluxdbService(dbname, retentionPolicy, influxDB);
            //创建数据库备份，设定存放时间以及备份数量
            service.createDatabase();
            service.createRetentionPolicy(time, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return service;
    }
}
